package com.dana.gomoku.controller;

import com.dana.gomoku.constant.Constants;
import com.dana.gomoku.model.Result;
import com.dana.gomoku.model.User;
import com.dana.gomoku.service.UserService;
import com.dana.gomoku.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 用户注册
     * @param username 用户名
     * @param password 密码
     */
    @PostMapping("/register")
    public Result register(String username, String password){
        //1.校验数据
        if (!StringUtils.hasLength(username)||!StringUtils.hasLength(password)){
            return Result.fail("账号或者密码为空");
        }

        //用户名不能小于4
        if (username.length()<4){
            return Result.fail("用户名不能小于4位");
        }
        //密码长度不能小于8
        if (password.length()<8){
            return Result.fail("密码长度不能小于8位");
        }

        //2.账号不能重复
        if(userService.querySelect(username)!=null){
            return Result.fail("用户名存在,请重新输入");
        }
        //正则表达式
        String pattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$";
        Matcher matcher = Pattern.compile(pattern).matcher(username);
        if (!matcher.find()){
            return Result.fail("用户名必须由字母和数字组成");
        }
        //3.密码加密
        String encrypt = SecurityUtils.encrypt(password);
        //4.将用户存在数据库就是注册
        Integer tep = userService.queryInsert(username, encrypt);


        return Result.success("注册成功,用户+ "+tep);
    }

    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @param request 设置session
     * @return 安全的User对象
     */
    @PostMapping("/login")
    public Result<User> login(String username, String password, HttpServletRequest request){
        if (!StringUtils.hasLength(username)||!StringUtils.hasLength(password)){
            return Result.fail("账号或者密码为空");
        }

        //用户名不能小于4
        if (username.length()<4){
            return Result.fail("用户名不能小于4位");
        }
        //密码长度不能小于8
        if (password.length()<8){
            return Result.fail("密码长度不能小于8位");
        }
        //校验是否存在该用户
        User user = userService.querySelect(username);
        if (user==null){
            return Result.fail("该用户不存在");
        }
        //校验密码
        boolean verity = SecurityUtils.verity(password, user.getPassword());
        if (verity==false){
            return Result.fail("密码错误");
        }
        //设置session
        request.getSession().setAttribute(Constants.USER_SESSION_KEY,user);
        User safetyUser=new User();
        safetyUser.setUserId(user.getUserId());
        safetyUser.setScore(user.getScore());
        safetyUser.setWinCount(user.getWinCount());
        safetyUser.setTotalCount(user.getTotalCount());
        safetyUser.setUsername(user.getUsername());
        return Result.success(safetyUser);
    }
    @GetMapping("/userInfo")
    public User getUserInfo(HttpServletRequest request){
        User safetyUser=new User();
        try {
            HttpSession session = request.getSession();
            User attribute = (User) session.getAttribute(Constants.USER_SESSION_KEY);
            safetyUser.setScore(attribute.getScore());
            safetyUser.setUsername(attribute.getUsername());
            safetyUser.setTotalCount(attribute.getTotalCount());
            safetyUser.setWinCount(attribute.getWinCount());
            safetyUser.setUserId(attribute.getUserId());
        }catch (NullPointerException e){
            return null;
        }

        return safetyUser;
    }
}
